package com.dss.auto_deploy.controller;

import com.dss.auto_deploy.domain.AdmRegistryTagHst;
import com.dss.auto_deploy.dto.DockerTagHstQueryDto;
import com.dss.auto_deploy.dto.RegistryTagHstPageDto;
import com.dss.auto_deploy.dto.SysFileDto;
import com.dss.auto_deploy.service.IAdmRegistryTagHstService;
import com.dss.auto_deploy.util.FileUtils;
import com.dss.auto_deploy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
* docker版本号维护
* @Description:docker版本号维护控制层
* @author dongshoushan
* @date 2022-10-08 11:29:20
*/
@RestController
@RequestMapping("/admRegistryTagHst")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(tags ="99: 版本记录")
public class AdmRegistryTagHstController {

    /**
    * docker版本号维护Service
    */
    private final  IAdmRegistryTagHstService admRegistryTagHstService;
    private final HttpServletResponse response;
    /**
     * 查询镜像版本
     * @param dto
     * @return
     */
    @PostMapping("/getTagHst")
    @ApiOperation(value = "查询镜像版本", notes="查询镜像版本")
    public  Result<List<AdmRegistryTagHst>> getTagHst(@RequestBody @Valid DockerTagHstQueryDto dto){
        List<AdmRegistryTagHst> list = admRegistryTagHstService.getTagHst(dto);
        return Result.success(list);
    }
    @GetMapping("/page")
    @ApiOperation(value = "版本记录", notes="版本记录")
    public Result page(RegistryTagHstPageDto pageDto){
        return Result.success(admRegistryTagHstService.list(pageDto));
    }
    @GetMapping("/exportImages")
    @ApiOperation(value = "导出镜像", notes="导出镜像")
    public void exportImages(String ids) throws Exception {
        String[] split = ids.split(",");
        List<String> idList = Arrays.asList(split);
        List<SysFileDto> pathList = admRegistryTagHstService.exportImages(idList);
        FileUtils.zipDirFileToFile(pathList,response);
    }
    @GetMapping("/deployProduction")
    @ApiOperation(value = "部署生产", notes="部署生产")
    public Result deployProduction(String ids) throws Exception {
        String[] split = ids.split(",");
        List<String> idList = Arrays.asList(split);
        admRegistryTagHstService.deployProduction(idList);
        return Result.success("部署成功");
    }


}
